﻿namespace Silent.API.Ukrbash
{
    public class BashQueryBuilder
    {
        public readonly string format;
        public readonly string apiKey;                       // API ключ клієнта, видається власнику програми/сервісу, обов’язковий

        public BashQueryBuilder(string _apiKey, string _format = "xml")
        {
            this.apiKey = _apiKey;
            this.format = _format;
        }

        #region Methods

        private string BuildBaseQuery(string section, string parameters = "")
        {
            return string.Format("{0}/{1}.{2}?{3}client={4}", QueryConstants.BASE_URL, section, this.format, parameters, this.apiKey);
        }
        private string BuildParametersQuery(params object[] parameters)
        {
            string result = string.Empty;
            for (int i = 0; i < parameters.Length; i += 2)
                if (i < parameters.Length)
                    result += string.Format("{0}={1}&", parameters[i], parameters[i + 1]);
            return result;
        }

        public string BuildPublishedQuotesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.QUOTES_PUBLISHED);
        }
        public string BuildUpcomingQuotesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.QUOTES_UPCOMING);
        }
        public string BuildTheBestQuotesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.QUOTES_THE_BEST);
        }
        public string BuildRandomQuotesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.QUOTES_RANDOM);
        }
        public string BuildPublishedPicturesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.PICTURES_PUBLISHED);
        }
        public string BuildUpcomingPicturesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.PICTURES_UPCOMING);
        }
        public string BuildTheBestPicturesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.PICTURES_THE_BEST);
        }
        public string BuildRandomPicturesQuery()
        {
            return this.BuildBaseQuery(QueryConstants.PICTURES_RANDOM);
        }

        public string BuildUserListQuery()
        {
            return this.BuildBaseQuery(QueryConstants.USER_LIST);
        }
        public string BuildSearchQuery(string search)
        {
            return this.BuildBaseQuery(QueryConstants.SITE_SEARCH, this.BuildParametersQuery(QueryConstants.PARAM_QUERY, search));
        }
        public string BuildTagsQuery()
        {
            return this.BuildBaseQuery(QueryConstants.SITE_TAGS);
        }

        public string BuildSiteInfoQuery()
        {
            return this.BuildBaseQuery(QueryConstants.SITE_INFO);
        }
        public string BuildQuoteByIdQuery(int id)
        {
            return this.BuildBaseQuery(QueryConstants.QUOTES_BY_ID, this.BuildParametersQuery(QueryConstants.PARAM_ID, id));
        }
        public string BuildPictureByIdQuery(int id)
        {
            return this.BuildBaseQuery(QueryConstants.PICTURES_BY_ID, this.BuildParametersQuery(QueryConstants.PARAM_ID, id));
        }
        public string BuildUserByIdQuery(int id)
        {
            return this.BuildBaseQuery(QueryConstants.USER_ID, this.BuildParametersQuery(QueryConstants.PARAM_ID, id));
        }

        #endregion
    }
}
